<?php
if ( ! function_exists('array_to_object'))
{
    function array_to_object($array)
    {
        if(!is_array($array))
        {
            return $array;
        }

        $object = new stdClass();
        if (is_array($array) && count($array) > 0)
        {
            foreach ($array as $name=>$value) {
                $name = strtolower(trim($name));
                if (!empty($name)) {
                    $object->$name = array_to_object($value);
                }
            }
            return $object;
        }
        else
        {
            return FALSE;
        }
    }
}

if ( ! function_exists('substr_words'))
{
    function substr_words($str, $txt_len, $end_txt = '...') {
        $words = explode(' ', $str);
        $count = 0;
        $new_str = '';
        $abbr = '';
        foreach ($words as $val) {
            if ($count < $txt_len) {
                $new_str .= $val.' ';
                $count = $count + strlen($val);
            }
        }
        $new_str = rtrim($new_str, ' ,.;:');
        if (strlen($str) > $txt_len) $new_str .= $end_txt;
        return $new_str;
    }
}

if ( ! function_exists('convert_vi_to_en'))
{
    function convert_vi_to_en($str,$remove_special_char = false)
    {
        $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
        $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
        $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
        $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
        $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
        $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
        $str = preg_replace("/(đ)/", 'd', $str);
        $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
        $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
        $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
        $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
        $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
        $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
        $str = preg_replace("/(Đ)/", 'D', $str);

        $str = strtolower($str);

        $result = '';
        if($remove_special_char)
        {
            for($a=0;$a<strlen($str);$a++)
            {
                if(($str[$a] >= 'a' && $str[$a] <= 'z') || ($str[$a] >= 'A' && $str[$a] <= 'Z') || ($str[$a] >= '0' && $str[$a] <= '9'))
                {
                    $result .= $str[$a];
                }
                else if($str[$a] == ' ' || $str[$a] == '.' || $str[$a] == ',' || $str[$a] == '/' || $str[$a] == '\\')
                {
                    $result .= '-';
                }
            }
            $result = str_replace('--','-',$result);
            $result = str_replace('---','-',$result);
            $result = str_replace('----','-',$result);
            $result = str_replace('-----','-',$result);
            $result = str_replace('------','-',$result);
        }
        else $result = $str;

        return $result;
    }
}

if ( ! function_exists('download_remote_file'))
{
    function download_remote_file($file_url, $save_to)
    {
        // Get file name from url.
        $pathinfo = pathinfo($file_url);
        $extension = $pathinfo['extension'];
        $filename = $pathinfo['filename'];

        // Get content from url
        $content = file_get_contents($file_url);

        // Prevent name conflict by add suffix for file (1, 2, 3...).
        $index = '';
        while(file_exists($save_to.$filename.$index.'.'.$extension))
        {
            if($index == '') $index = 2;
            else $index++;
        }

        $file_path = $save_to.$filename.$index.'.'.$extension;

        file_put_contents($file_path, $content);

        return $file_path;
    }
}

if ( ! function_exists('get_filename_from_fullname'))
{
    function get_filename_from_fullname($fullname)
    {
        // Get file name from url.
        $pathinfo = pathinfo($fullname);
        $extension = $pathinfo['extension'];
        $filename = $pathinfo['filename'];
        return "$filename.$extension";
    }
}
